<template>
  <el-dialog :title="$t('workOrderMg.colseOrder')" :visible="visible" width="400px" @close="onClose">
    <el-form
      :ref="refName"
      :model="ruleForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item :label="$t('workOrderMg.handling')" prop="dealSituationCode">
        <el-select v-model="ruleForm.dealSituationCode" :placeholder="$t('workOrderMg.choose')">
          <el-option
            v-for="(item, index) in workOrderDealList"
            :key="index"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('workOrderMg.belonging')" prop="attributiveCode">
        <el-select v-model="ruleForm.attributiveCode" :placeholder="$t('workOrderMg.choose')">
          <el-option
            v-for="(item, index) in workOrderAttributiveList"
            :key="index"
            :label="item.dictValue"
            :value="item.dictKey"
          />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('workOrderMg.reason')" prop="reason">
        <el-input v-model="ruleForm.reason" />
      </el-form-item>
      <el-form-item :label="$t('workOrderMg.remark')" prop="remark">
        <el-input
          v-model="ruleForm.remark"
          type="textarea"
          autosize
          :placeholder="$t('workOrderMg.inputContent')"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="loading" @click="submitForm">
        {{ $t('btns.confirm') }}
      </el-button>
      <el-button @click="onClose">
        {{ $t('btns.cancel') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  props: {
    visible: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      refName: 'ruleForm',
      ruleForm: {},
      rules: {
        dealSituationCode: [
          { required: true, message: this.$t('workOrderMg.inputHandling'), trigger: 'blur' }
        ],
        attributiveCode: [{ required: true, message: this.$t('workOrderMg.inputBelonging'), trigger: 'blur' }]
      },
    }
  },
  computed: {
    ...mapGetters(['dicMap', 'dicCodes', 'loading']),
    workOrderDealList() {
      return this.dicMap[this.dicCodes.workOrderDeal]
    },
    workOrderAttributiveList() {
      return this.dicMap[this.dicCodes.workOrderAttributive]
    }
  },
  //   workOrderAttributive: 'workOrderAttributive', // 问题归属
  // workOrderDeal: 'workOrderDeal' // 处理情况
  mounted() {
    this.$store.dispatch(
      'common/getI18nDict',
      this.dicCodes.workOrderAttributive
    )
    this.$store.dispatch('common/getI18nDict', this.dicCodes.workOrderDeal)
  },
  methods: {
    submitForm(ruleForm) {
      this.$refs[this.refName].validate(valid => {
        if (!valid) {
          return
        }
        this.$store
          .dispatch('workOrder/onWoClose', this.ruleForm)
          .then(success => {
            if (success) {
              this.onClose()
            }
          })
      })
    },
    onClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>
